import { loginRequest, getUserInfoByTokenRequest,logoutRequest } from "@/api";

export default {
  /**@getToken 获取登陆后返回的信息 */
  async getToken({ commit }, userInfo) {
    const result = await loginRequest(userInfo);
    localStorage.setItem("user_token",result.token)
    commit("SET_TOKEN_STATE", result.token);
  },
  /**@getNickName 获取登陆后返回的信息 */
  async getNickName({ commit }, nickName) {
    try {
    const result = await getUserInfoByTokenRequest(nickName);
    commit("SET_NICKNAME_STATE", result.nickName);
  } catch (e) {
    //当token过期发送请求会报错,报错就把过期的token删掉
    localStorage.removeItem("user_token")
    return Promise.reject()
  }
  },
  /**@getLogout 退出登陆的事件*/
  async getLogout({ commit }){
    await logoutRequest()
    commit("CLEAR_USER_INFO")
  }
};
